package o;

import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import okhttp3.internal.connection.RealConnection;
import okhttp3.internal.connection.StreamAllocation;

public final class fwe implements fvv {
    private long ʼ = 262144;
    int ˊ = 0;
    final fuv ˋ;
    final fxj ˎ;
    final StreamAllocation ˏ;
    final fxg ॱ;

    abstract class d implements fxw {
        protected final fxl ˋ;
        protected long ˎ;
        protected boolean ˏ;
        final /* synthetic */ fwe ॱ;

        private d(fwe o_fwe) {
            this.ॱ = o_fwe;
            this.ˋ = new fxl(this.ॱ.ॱ.ˊ());
            this.ˎ = 0;
        }

        public fxv ˊ() {
            return this.ˋ;
        }

        public long ˊ(fxi o_fxi, long j) throws IOException {
            try {
                long ˊ = this.ॱ.ॱ.ˊ(o_fxi, j);
                if (ˊ > 0) {
                    this.ˎ += ˊ;
                }
                return ˊ;
            } catch (IOException e) {
                ॱ(false, e);
                throw e;
            }
        }

        protected final void ॱ(boolean z, IOException iOException) throws IOException {
            if (this.ॱ.ˊ != 6) {
                if (this.ॱ.ˊ != 5) {
                    throw new IllegalStateException("state: " + this.ॱ.ˊ);
                }
                this.ॱ.ˎ(this.ˋ);
                this.ॱ.ˊ = 6;
                if (this.ॱ.ˏ != null) {
                    this.ॱ.ˏ.streamFinished(!z, this.ॱ, this.ˎ, iOException);
                }
            }
        }
    }

    class a extends d {
        private boolean ʻ = true;
        private final fur ʼ;
        final /* synthetic */ fwe ˊ;
        private long ॱॱ = -1;

        a(fwe o_fwe, fur o_fur) {
            this.ˊ = o_fwe;
            super();
            this.ʼ = o_fur;
        }

        public long ˊ(fxi o_fxi, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException("byteCount < 0: " + j);
            } else if (this.ˏ) {
                throw new IllegalStateException("closed");
            } else if (!this.ʻ) {
                return -1;
            } else {
                if (this.ॱॱ == 0 || this.ॱॱ == -1) {
                    ˏ();
                    if (!this.ʻ) {
                        return -1;
                    }
                }
                long ˊ = super.ˊ(o_fxi, Math.min(j, this.ॱॱ));
                if (ˊ == -1) {
                    IOException protocolException = new ProtocolException("unexpected end of stream");
                    ॱ(false, protocolException);
                    throw protocolException;
                }
                this.ॱॱ -= ˊ;
                return ˊ;
            }
        }

        private void ˏ() throws IOException {
            if (this.ॱॱ != -1) {
                this.ˊ.ॱ.ॱˋ();
            }
            try {
                this.ॱॱ = this.ˊ.ॱ.ᐝॱ();
                String trim = this.ˊ.ॱ.ॱˋ().trim();
                if (this.ॱॱ < 0 || !(trim.isEmpty() || trim.startsWith(";"))) {
                    throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.ॱॱ + trim + "\"");
                } else if (this.ॱॱ == 0) {
                    this.ʻ = false;
                    fvr.ॱ(this.ˊ.ˋ.ॱॱ(), this.ʼ, this.ˊ.ˏ());
                    ॱ(true, null);
                }
            } catch (NumberFormatException e) {
                throw new ProtocolException(e.getMessage());
            }
        }

        public void close() throws IOException {
            if (!this.ˏ) {
                if (this.ʻ && !fvg.ˋ((fxw) this, 100, TimeUnit.MILLISECONDS)) {
                    ॱ(false, null);
                }
                this.ˏ = true;
            }
        }
    }

    final class b implements fxx {
        private boolean ˊ;
        private long ˋ;
        private final fxl ˎ = new fxl(this.ˏ.ˎ.ˊ());
        final /* synthetic */ fwe ˏ;

        b(fwe o_fwe, long j) {
            this.ˏ = o_fwe;
            this.ˋ = j;
        }

        public fxv ˊ() {
            return this.ˎ;
        }

        public void ˎ(fxi o_fxi, long j) throws IOException {
            if (this.ˊ) {
                throw new IllegalStateException("closed");
            }
            fvg.ˎ(o_fxi.ॱ(), 0, j);
            if (j > this.ˋ) {
                throw new ProtocolException("expected " + this.ˋ + " bytes but received " + j);
            }
            this.ˏ.ˎ.ˎ(o_fxi, j);
            this.ˋ -= j;
        }

        public void flush() throws IOException {
            if (!this.ˊ) {
                this.ˏ.ˎ.flush();
            }
        }

        public void close() throws IOException {
            if (!this.ˊ) {
                this.ˊ = true;
                if (this.ˋ > 0) {
                    throw new ProtocolException("unexpected end of stream");
                }
                this.ˏ.ˎ(this.ˎ);
                this.ˏ.ˊ = 3;
            }
        }
    }

    class c extends d {
        private long ʼ;
        final /* synthetic */ fwe ˊ;

        c(fwe o_fwe, long j) throws IOException {
            this.ˊ = o_fwe;
            super();
            this.ʼ = j;
            if (this.ʼ == 0) {
                ॱ(true, null);
            }
        }

        public long ˊ(fxi o_fxi, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException("byteCount < 0: " + j);
            } else if (this.ˏ) {
                throw new IllegalStateException("closed");
            } else if (this.ʼ == 0) {
                return -1;
            } else {
                long ˊ = super.ˊ(o_fxi, Math.min(this.ʼ, j));
                if (ˊ == -1) {
                    IOException protocolException = new ProtocolException("unexpected end of stream");
                    ॱ(false, protocolException);
                    throw protocolException;
                }
                this.ʼ -= ˊ;
                if (this.ʼ == 0) {
                    ॱ(true, null);
                }
                return ˊ;
            }
        }

        public void close() throws IOException {
            if (!this.ˏ) {
                if (!(this.ʼ == 0 || fvg.ˋ((fxw) this, 100, TimeUnit.MILLISECONDS))) {
                    ॱ(false, null);
                }
                this.ˏ = true;
            }
        }
    }

    final class e implements fxx {
        private boolean ˊ;
        final /* synthetic */ fwe ˋ;
        private final fxl ˎ = new fxl(this.ˋ.ˎ.ˊ());

        e(fwe o_fwe) {
            this.ˋ = o_fwe;
        }

        public fxv ˊ() {
            return this.ˎ;
        }

        public void ˎ(fxi o_fxi, long j) throws IOException {
            if (this.ˊ) {
                throw new IllegalStateException("closed");
            } else if (j != 0) {
                this.ˋ.ˎ.ॱˊ(j);
                this.ˋ.ˎ.ˋ("\r\n");
                this.ˋ.ˎ.ˎ(o_fxi, j);
                this.ˋ.ˎ.ˋ("\r\n");
            }
        }

        public synchronized void flush() throws IOException {
            if (!this.ˊ) {
                this.ˋ.ˎ.flush();
            }
        }

        public synchronized void close() throws IOException {
            if (!this.ˊ) {
                this.ˊ = true;
                this.ˋ.ˎ.ˋ("0\r\n\r\n");
                this.ˋ.ˎ(this.ˎ);
                this.ˋ.ˊ = 3;
            }
        }
    }

    class h extends d {
        private boolean ʼ;
        final /* synthetic */ fwe ˊ;

        h(fwe o_fwe) {
            this.ˊ = o_fwe;
            super();
        }

        public long ˊ(fxi o_fxi, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException("byteCount < 0: " + j);
            } else if (this.ˏ) {
                throw new IllegalStateException("closed");
            } else if (this.ʼ) {
                return -1;
            } else {
                long ˊ = super.ˊ(o_fxi, j);
                if (ˊ != -1) {
                    return ˊ;
                }
                this.ʼ = true;
                ॱ(true, null);
                return -1;
            }
        }

        public void close() throws IOException {
            if (!this.ˏ) {
                if (!this.ʼ) {
                    ॱ(false, null);
                }
                this.ˏ = true;
            }
        }
    }

    public fwe(fuv o_fuv, StreamAllocation streamAllocation, fxg o_fxg, fxj o_fxj) {
        this.ˋ = o_fuv;
        this.ˏ = streamAllocation;
        this.ॱ = o_fxg;
        this.ˎ = o_fxj;
    }

    public fxx ॱ(fus o_fus, long j) {
        if ("chunked".equalsIgnoreCase(o_fus.ˋ("Transfer-Encoding"))) {
            return ˎ();
        }
        if (j != -1) {
            return ˋ(j);
        }
        throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!");
    }

    public void ˋ() {
        RealConnection connection = this.ˏ.connection();
        if (connection != null) {
            connection.cancel();
        }
    }

    public void ˋ(fus o_fus) throws IOException {
        ˏ(o_fus.ॱ(), fwa.ॱ(o_fus, this.ˏ.connection().route().proxy().type()));
    }

    public fuy ˎ(fvb o_fvb) throws IOException {
        this.ˏ.eventListener.responseBodyStart(this.ˏ.call);
        String ˋ = o_fvb.ˋ(HttpContants.KEY_CONTENT_TYPE);
        if (!fvr.ˊ(o_fvb)) {
            return new fvx(ˋ, 0, fxr.ˎ(ˏ(0)));
        }
        if ("chunked".equalsIgnoreCase(o_fvb.ˋ("Transfer-Encoding"))) {
            return new fvx(ˋ, -1, fxr.ˎ(ˊ(o_fvb.ॱ().ˏ())));
        }
        long ˏ = fvr.ˏ(o_fvb);
        if (ˏ != -1) {
            return new fvx(ˋ, ˏ, fxr.ˎ(ˏ(ˏ)));
        }
        return new fvx(ˋ, -1, fxr.ˎ(ᐝ()));
    }

    public void ˊ() throws IOException {
        this.ˎ.flush();
    }

    public void ॱ() throws IOException {
        this.ˎ.flush();
    }

    public void ˏ(fui o_fui, String str) throws IOException {
        if (this.ˊ != 0) {
            throw new IllegalStateException("state: " + this.ˊ);
        }
        this.ˎ.ˋ(str).ˋ("\r\n");
        int ˋ = o_fui.ˋ();
        for (int i = 0; i < ˋ; i++) {
            this.ˎ.ˋ(o_fui.ॱ(i)).ˋ(": ").ˋ(o_fui.ˋ(i)).ˋ("\r\n");
        }
        this.ˎ.ˋ("\r\n");
        this.ˊ = 1;
    }

    public o.fvb.d ˏ(boolean z) throws IOException {
        if (this.ˊ == 1 || this.ˊ == 3) {
            try {
                fvy ॱ = fvy.ॱ(ʻ());
                o.fvb.d ॱ2 = new o.fvb.d().ॱ(ॱ.ˋ).ॱ(ॱ.ˎ).ˊ(ॱ.ˊ).ॱ(ˏ());
                if (z && ॱ.ˎ == 100) {
                    return null;
                }
                if (ॱ.ˎ == 100) {
                    this.ˊ = 3;
                    return ॱ2;
                }
                this.ˊ = 4;
                return ॱ2;
            } catch (Throwable e) {
                IOException iOException = new IOException("unexpected end of stream on " + this.ˏ);
                iOException.initCause(e);
                throw iOException;
            }
        }
        throw new IllegalStateException("state: " + this.ˊ);
    }

    private String ʻ() throws IOException {
        String ˊ = this.ॱ.ˊ(this.ʼ);
        this.ʼ -= (long) ˊ.length();
        return ˊ;
    }

    public fui ˏ() throws IOException {
        o.fui.a aVar = new o.fui.a();
        while (true) {
            String ʻ = ʻ();
            if (ʻ.length() == 0) {
                return aVar.ˊ();
            }
            fux.ˏ.ˊ(aVar, ʻ);
        }
    }

    public fxx ˎ() {
        if (this.ˊ != 1) {
            throw new IllegalStateException("state: " + this.ˊ);
        }
        this.ˊ = 2;
        return new e(this);
    }

    public fxx ˋ(long j) {
        if (this.ˊ != 1) {
            throw new IllegalStateException("state: " + this.ˊ);
        }
        this.ˊ = 2;
        return new b(this, j);
    }

    public fxw ˏ(long j) throws IOException {
        if (this.ˊ != 4) {
            throw new IllegalStateException("state: " + this.ˊ);
        }
        this.ˊ = 5;
        return new c(this, j);
    }

    public fxw ˊ(fur o_fur) throws IOException {
        if (this.ˊ != 4) {
            throw new IllegalStateException("state: " + this.ˊ);
        }
        this.ˊ = 5;
        return new a(this, o_fur);
    }

    public fxw ᐝ() throws IOException {
        if (this.ˊ != 4) {
            throw new IllegalStateException("state: " + this.ˊ);
        } else if (this.ˏ == null) {
            throw new IllegalStateException("streamAllocation == null");
        } else {
            this.ˊ = 5;
            this.ˏ.noNewStreams();
            return new h(this);
        }
    }

    void ˎ(fxl o_fxl) {
        fxv ˎ = o_fxl.ˎ();
        o_fxl.ॱ(fxv.ॱ);
        ˎ.ॱॱ();
        ˎ.ˊ();
    }
}
